package it.okkam.longwell.locale;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.PropertyResourceBundle;

public class Localize {
	
	//Languages supported
	public static final String EN_LOCALE = "en";
	
	public static final String IT_LOCALE = "it";
	
	private String _language = null;  // "it" or "en"
	
	private HashMap<String,String> _locales = null; // Map of (languages, property file) pairs
	
	private InputStream in = null;
	
	private PropertyResourceBundle propsBundle = null;
	
	public Localize(HashMap locales){
		_locales = locales;
	}
	
	public void setLocale(String language){
		_language = language;
		
		setPropertiesFile(_language);
	}
	
	public String getLocale(){
		return _language;
	}
	
	public PropertyResourceBundle getProperties() {
		return propsBundle;
	} 
	
	private void setPropertiesFile(String language){
		
		unsetLocale();
		
		String propertiesFile = _locales.get(language);
		
		try {
			
			in = new FileInputStream( propertiesFile );
			
			propsBundle = new PropertyResourceBundle(in);
				
			
		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
		}	
		catch (IOException ioe) {
			
			ioe.printStackTrace();
		}		
		
	}
	
	public void unsetLocale(){
		
		if(in != null){
			try {
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

}
